package isae.c2.android.depense;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import isae.c2.android.depense.DB.CategoryDBAdapter;

public class create_category extends Activity {

        private EditText mTitleText;
        private EditText mBodyText;
        private Integer mSave;
        private Long mRowId;
        private Spinner mCatType;
        private CategoryDBAdapter mDbHelper;

        @Override
        protected void onCreate(Bundle bundle) {
            mSave=1; // Ajouter pour ne pas inserer une nouvelle categorie si
                    // l'utilisateur n'a pas appuyer sur le bouton save
            super.onCreate(bundle);
            mDbHelper = new CategoryDBAdapter(this);
            mDbHelper.open();
            setContentView(R.layout.create_category);
            mTitleText = (EditText) findViewById(R.id.todo_edit_summary);
            mBodyText = (EditText) findViewById(R.id.todo_edit_description);
            mCatType = (Spinner) findViewById(R.id.cat_type);
            
            Button confirmButton = (Button) findViewById(R.id.todo_edit_button);
            mRowId = null;
            Bundle extras = getIntent().getExtras();
            mRowId = (bundle == null) ? null : (Long) bundle
                    .getSerializable(CategoryDBAdapter.KEY_ROWID);
            if (extras != null) {
                mRowId = extras.getLong(CategoryDBAdapter.KEY_ROWID);
            } 
            
            populateFields();
            confirmButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) { 
                    mSave = 0; // L'utilisateur a appuyer sur le bouton "Eregistrer"
                    setResult(RESULT_OK);
                    finish();
                }

            });
        }
        
        private void populateFields() { 
            if (mRowId != null) {
                Cursor todo = mDbHelper.fetchTodo(mRowId);
                startManagingCursor(todo);
                
                String CatType = todo.getString(todo
                        .getColumnIndexOrThrow(CategoryDBAdapter.KEY_CATTYPE));
                
                for (int i=0; i<mCatType.getCount();i++){
                    
                    String s = (String) mCatType.getItemAtPosition(i); 
                    
                    if (s.equalsIgnoreCase(CatType)){
                        mCatType.setSelection(i);
                    }
                }
                mTitleText.setText(todo.getString(todo
                        .getColumnIndexOrThrow(CategoryDBAdapter.KEY_CODE)));
                mBodyText.setText(todo.getString(todo
                        .getColumnIndexOrThrow(CategoryDBAdapter.KEY_DESCRIPTION)));
            }
        }
        
        
        protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            saveState();
            outState.putSerializable(CategoryDBAdapter.KEY_ROWID, mRowId);
        }

        @Override
        protected void onPause() {  
            
            super.onPause();
            
            if (mSave==0) {
                saveState();
            }
        }

        @Override
        protected void onResume() { 
            super.onResume();
            populateFields();
        }
        
        private void saveState() { 
            String CatType = (String) mCatType.getSelectedItem();
            String code = mTitleText.getText().toString();
            String description = mBodyText.getText().toString();

            if (mRowId == null){
                
                if (!code.equals("")  && !description.equals("")) { // Pour ne pas inserer
                // une categorie qui a le code et la description vides
                    long id = mDbHelper.createTodo(CatType, code, description);
                    if (id > 0) {
                        mRowId = id;
                    }
                }
            } else {
                mDbHelper.updateTodo(mRowId, CatType, code, description);
            }
        }

}

